package com.ccb.jyzx.expand.ouapi.service.impl;

import com.ccb.jyzx.expand.ouapi.beans.*;
import com.ccb.jyzx.expand.ouapi.beans.sync.UserInfoSync;
import com.ccb.jyzx.expand.ouapi.dao.ouapi.DepartmentDao;
import com.ccb.jyzx.expand.ouapi.dao.ouapi.UserInfoDao;
import com.ccb.jyzx.expand.ouapi.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XIEFEI
 * @date 2019-11-11 14:40
 * @Description
 **/
@Service
public class UserInfoServiceImpl implements UserInfoService {



    @Autowired
    UserInfoDao userInfoDao;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    DepartmentDao departmentDao;

    @Autowired
    OrganiztionService organiztionService;

    @Autowired
    DutyService dutyService;

    @Autowired
    UserDutyRefService userInfoDutyInfoRefService;


    @Override
    public UserInfo getById(String id) {
        UserInfo userInfo = userInfoDao.getById(id);
        facthDepartmentInfo(userInfo);//关联部门信息
        facthOrganiztionInfo(userInfo);//关联组织信息
        facthDutyInfo(userInfo);//关联岗位信息

        return userInfo;
    }


    @Override
    public UserInfo getByUserName(String userName) {
        UserInfo userInfo = userInfoDao.getByUserName(userName);
        facthDepartmentInfo(userInfo);//关联部门信息
        facthOrganiztionInfo(userInfo);//关联组织信息
        facthDutyInfo(userInfo);//关联岗位信息

        return userInfo;
    }

    @Override
    public List<UserInfo> getByDepId(String depId) {
        DepartmentInfo departmentInfo = departmentService.getById(depId);
        List<UserInfo> byDepId = userInfoDao.getByDepId(depId);
        List<OrganiztionInfo> orgList = organiztionService.getAll();

        if (departmentInfo != null && byDepId != null && !byDepId.isEmpty()) {
            byDepId.forEach(userInfo -> {
                userInfo.setDepartmentInfo(departmentInfo);
                if (orgList != null && !orgList.isEmpty() && StringUtils.isNotBlank(userInfo.getOwnerOrganization())) {
                    orgList.stream().filter(organiztionInfo -> {
                        return organiztionInfo.getId().equals(userInfo.getOwnerOrganization());
                    }).forEach(organiztionInfo -> {
                        userInfo.setOrganiztionInfo(organiztionInfo);
                    });
                }
            });
        }

        return byDepId;
    }

    @Override
    public List<UserInfo> getByOrgId(String orgId) {
        OrganiztionInfo organiztionInfo = organiztionService.getById(orgId);
        List<UserInfo> byOrgId = userInfoDao.getByOrgId(orgId);
        List<DepartmentInfo> depList = departmentService.getAll();
        if (byOrgId != null && !byOrgId.isEmpty() && organiztionInfo != null) {
            byOrgId.forEach(userInfo -> {
                userInfo.setOrganiztionInfo(organiztionInfo);
                if (depList != null && !depList.isEmpty() && StringUtils.isNotBlank(userInfo.getOwnerDept())) {
                    depList.stream().filter(departmentInfo -> {
                        return departmentInfo.getId().equals(userInfo.getOwnerDept());
                    }).forEach(departmentInfo -> {
                        userInfo.setDepartmentInfo(departmentInfo);
                    });
                }
            });
        }
        return byOrgId;
    }

    @Override
    public List<UserInfoSync> getAllSync() {
        return userInfoDao.getAllSync();
    }

    @Override
    public void insert(UserInfoSync userInfoSync) {
        userInfoDao.save(userInfoSync);
    }

    @Override
    public void update(UserInfoSync userInfoSync) {
        userInfoDao.update(userInfoSync);
    }

    @Override
    public void deleteById(String id) {
        userInfoDao.deleteById(id);
    }

    /**
     * 关联查询部门信息
     *
     * @return
     */
    private void facthDepartmentInfo(UserInfo userInfo) {
        if (userInfo != null && StringUtils.isNotBlank(userInfo.getOwnerDept())) {
            DepartmentInfo departmentInfo = departmentService.getById(userInfo.getOwnerDept());
            if (departmentInfo != null) {
                userInfo.setDepartmentInfo(departmentInfo);
            }
        }
    }

    /**
     * 关联查询组织信息
     *
     * @param userInfo
     */
    private void facthOrganiztionInfo(UserInfo userInfo) {
        if (userInfo != null && StringUtils.isNotBlank(userInfo.getOwnerOrganization())) {
            OrganiztionInfo organiztionInfo = organiztionService.getById(userInfo.getOwnerOrganization());
            if (organiztionInfo != null) {
                userInfo.setOrganiztionInfo(organiztionInfo);
            }
        }
    }

    /**
     * 关联查询岗位信息
     *
     * @param userInfo
     */
    private void facthDutyInfo(UserInfo userInfo) {
        if (userInfo != null ) {
           List<UserDutyRef> list  = userInfoDutyInfoRefService.findByUserId(userInfo.getId());
           if (list!=null&&!list.isEmpty()){
               List<DutyInfo> dutyInfos= new ArrayList<>();
               list.forEach(d->{
                   DutyInfo byId = dutyService.getById(d.getOrgDeptDutyId());

                   if (byId!=null){
                       byId.setFlag(d.getFlag());
                       DepartmentInfo dep = departmentDao.getById(byId.getOrgDeptId());//查询岗位所在部门
                       if(dep!=null){
                            byId.setDepartmentInfo(dep);
                       }
                       dutyInfos.add(byId);
                   }
               });
               if (!dutyInfos.isEmpty()){
                   userInfo.setDutyInfo(dutyInfos);
               }
           }

        }
    }
}
